<?php


namespace App\Http\Controllers\Api\User;


use App\Http\Controllers\Api\BasicController;
use App\Models\SiteCode;
use App\Models\User;
use App\Repositories\ResponseCode;
use App\Services\Sms;
use Illuminate\Support\Facades\Hash;
use Jiannei\Response\Laravel\Support\Facades\Response;


class RegisterController extends BasicController
{


    /**
     * @api {post} /user/register/mobile 手机号注册
     * @apiName UserRegisterMobile
     * @apiGroup UserRegister
     * @apiDescription 用户手机号注册
     *
     * @apiParam {Number} mobile  用户注册手机号
     * @apiParam {Number} code  用户验证码
     * @apiParam {String} password  用户密码
     * @apiParam {String} [invite_code]  邀请码
     *
     */
    public function mobile()
    {
        $data = $this->data(['mobile', 'password', 'code', 'invite_code']);
        $where['mobile'] = $data['mobile'];
        $user = (new User())->getUser($where);
        if ($user)
            Response::fail(__('api.USER_EXIST'), ResponseCode::USER_EXIST);
        if (!empty($data['invite_code'])) {
            $where['invite_code'] = $data['invite_code'];
            $invite = (new User())->getUser($where);
            if (empty($invite))
                Response::fail(__('api.INVITE_ERROR'), ResponseCode::INVITE_ERROR);
            $insert['pid'] = $invite->id;
        }
       // (new Sms())->check($data['mobile'], $data['code'], SiteCode::CODE_REG);
        $insert['mobile'] = $data['mobile'];
        $insert['password'] = Hash::make($data['password']);
        $user = (new User())->createUser($insert);
        $token =$user->createToken($user->id)->plainTextToken;
        return Response::success($token);
    }
}
